package com.wq.rentHouse.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wq.rentHouse.entity.HouseEntity;
import com.wq.rentHouse.service.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wq.rentHouse.entity.UserEntity;
import com.wq.rentHouse.service.UserService;
import com.wq.common.utils.PageUtils;
import com.wq.common.utils.R;


/**
 * @author wuyunpeng
 * @email 2874165407@qq.com
 * @date 2022-09-06 08:37:14
 */
@RestController
@RequestMapping("rentHouse/user" )
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private HouseService houseService;

    //    @RequestMapping("/backDisplay" )
//    public R backDisplay(String ids) {
//        String[] split = ids.split("," );
//        List<Integer> idsParams = new ArrayList<>();
//        for (String s : split) {
//            idsParams.add(Integer.valueOf(s));
//        }
//        final List<UserEntity> userEntities = userService.listByIds(idsParams);
//        System.out.println(userEntities);
//        return R.ok().put("userEntities", userEntities);
//    }
    @RequestMapping("/backDisplay" )
    public R backDisplay(@RequestParam List<Integer> ids) {
        List<UserEntity> userEntities = userService.listByIds(ids);
        return R.ok().put("userEntities", userEntities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list" )
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = userService.queryPage(params);
//        final long pageIndex = Long.parseLong(String.valueOf(params.get("page" )));
//        final long pageSize = Long.parseLong(String.valueOf(params.get("limit" )));
////        final String key = (String) params.get("key" );
//        final Page<UserEntity> pageInfo = new Page<>(pageIndex, pageSize);
//        final LambdaQueryWrapper<UserEntity> lqw = new LambdaQueryWrapper<>();
//        lqw.orderByAsc(UserEntity::getId);
//        userService.page(pageInfo, lqw);
//        final PageUtils page = new PageUtils(pageInfo);
        return R.ok().put("page", page);
    }


    //        userEntities = userEntities.stream().map((item) -> {
//            final LambdaQueryWrapper<HouseEntity> lqw = new LambdaQueryWrapper<>();
//            if (item.getRentouts() != null) {
//                final String str = item.getRentouts();
//                final String[] split = str.split("," );
//                lqw.in(HouseEntity::getId, Arrays.asList(split));
//                final List<HouseEntity> rentsOutList = houseService.list(lqw);
//                final ArrayList<String> stringArrayList = new ArrayList<>();
//                rentsOutList.stream().map((item1) -> {
//                    stringArrayList.add(item1.getName());
//                    return item1;
//                }).collect(Collectors.toList());
//                final String rentsOutString = stringArrayList.toString();
//                item.setRentoutsList(rentsOutString);
//            } else if (item.getRents() != null) {
//                final String str = item.getRents();
//                final String[] split = str.split("," );
//                lqw.in(HouseEntity::getId, Arrays.asList(split));
//                final List<HouseEntity> rentsList = houseService.list(lqw);
//                final ArrayList<String> stringArrayList = new ArrayList<>();
//                rentsList.stream().map((item1) -> {
//                    stringArrayList.add(item1.getName());
//                    return item1;
//                }).collect(Collectors.toList());
//                final String rentsString = stringArrayList.toString();
//                item.setRentsList(rentsString);
//            }
//            return item;
//        }).collect(Collectors.toList());

    /**
     * 信息
     */
    @RequestMapping("/info/{id}" )
    public R info(@PathVariable("id" ) Long id) {
        UserEntity user = userService.getById(id);
        /**
         * 处理用户出租和租房
         */
        final LambdaQueryWrapper<HouseEntity> lqw = new LambdaQueryWrapper<>();
        if (user.getRentouts() != null) {
            final String str = user.getRentouts();
            final String[] split = str.split("," );
            lqw.in(HouseEntity::getId, Arrays.asList(split));
            final List<HouseEntity> rentsOutList = houseService.list(lqw);
            user.setRentoutsList(rentsOutList);
        }
        if (user.getRents() != null) {
            final String str = user.getRents();
            final String[] split = str.split("," );
            lqw.in(HouseEntity::getId, Arrays.asList(split));
            final List<HouseEntity> rentsList = houseService.list(lqw);
            user.setRentsList(rentsList);
        }

        /**
         * 封装所有的房屋
         */
        final List<HouseEntity> houseEntities = houseService.list();
        user.setAllHouse(houseEntities);
        return R.ok().put("user", user);
    }


    /**
     * 保存
     */
    @RequestMapping("/save" )
    public R save(@RequestBody UserEntity user) {
        userService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update" )
    public R update(@RequestBody UserEntity user) {
        userService.updateById(user);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete" )
    public R delete(@RequestParam List<Long> ids) {
//        userService.removeByIds(Arrays.asList(ids));
        System.out.println("ids " + ids);
        //逻辑删除 ids 的用户
        List<UserEntity> userEntities = new ArrayList<>();
        for (Long id : ids) {
            final UserEntity userEntity = new UserEntity();
            userEntity.setId(id);
            userEntity.setDisplay(0);
            userEntities.add(userEntity);
        }

        userService.updateBatchById(userEntities);
        return R.ok();
    }

}
